package com.beimin.evedata.hibernate;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.beimin.evedata.model.map.MapConstellation;
import com.beimin.evedata.model.map.MapLocation;
import com.beimin.evedata.model.map.MapRegion;
import com.beimin.evedata.model.map.MapSolarSystem;
import com.beimin.evedata.repository.MapRepository;

public class HibMapRepository implements MapRepository {
	protected final Logger logger = Logger.getLogger(getClass());
	protected final SessionFactory sf;

	public HibMapRepository(SessionFactory sf) {
		this.sf = sf;
	}

	public MapRegion getRegion(int regionID) {
		Session session = sf.getCurrentSession();
		return (MapRegion) session.get(MapRegion.class, regionID);
	}

	public MapConstellation getConstellation(int constelationID) {
		Session session = sf.getCurrentSession();
		return (MapConstellation) session.get(MapConstellation.class, constelationID);
	}

	public MapSolarSystem getSolarSystem(int solarSystemID) {
		Session session = sf.getCurrentSession();
		return (MapSolarSystem) session.get(MapSolarSystem.class, solarSystemID);
	}

	public void save(Object object) {
		Session session = sf.getCurrentSession();
		session.save(object);
	}

	public MapLocation getMapLocation(int itemID) {
		Session session = sf.getCurrentSession();
		try {
			return (MapLocation) session.get(MapLocation.class, itemID);
		} catch (Exception e) {
			logger.warn(e.getMessage());
			return null;
		}
	}
}
